package mod.luckymod.entity;

import mod.luckymod.ModBlocks;
import mod.luckymod.ModEntities;
import mod.luckymod.blocks.SmallSodiumBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

public class SodiumEntity extends FallingBlockEntity {
    public final BlockState blockState = ModBlocks.SMALL_SODIUM.getDefaultState();

    public SodiumEntity(EntityType<? extends FallingBlockEntity> type, Level level) {
        super(type, level);
    }
    public SodiumEntity create(Level level, BlockPos pos) {
        SodiumEntity entity = new SodiumEntity(ModEntities.SodiumEntity.get(), level);
        this.setStartPos(pos);
        this.setPos(getStartPos().getX(), getStartPos().getY(), getStartPos().getZ());
        return entity;
    }
    @Override
    public void tick() {
        if(this.level().isClientSide())return;
        if (this.onGround()) {
            BlockPos currentPos = this.blockPosition();
            Level level = this.level();
            BlockState blockStateAtPos = level.getBlockState(currentPos);

            // 情况1：落地位置是小型钠块，尝试增加堆叠数量
            if (blockStateAtPos.getBlock() instanceof SmallSodiumBlock) {
                int currentCount = blockStateAtPos.getValue(SmallSodiumBlock.COUNT);

                // 子情况1a：当前堆叠数小于8，可以增加
                if (currentCount < 8) {
                    int newCount = currentCount + 1;
                    BlockState newState = blockStateAtPos.setValue(SmallSodiumBlock.COUNT, newCount);

                    // 子情况1a-i：增加后刚好达到8个，转换为完整的钠方块
                    if (newCount == 8) {
                        newState = ModBlocks.SODIUM_BLOCK.get().defaultBlockState();
                    }
                    // 更新方块状态，并丢弃实体
                    level.setBlock(currentPos, newState, Block.UPDATE_ALL);
                    this.kill();
                }
            }
            // 情况2：落地位置可被替换（如空气、草等），放置一个新的小钠块
            else if (level.getBlockState(currentPos).canBeReplaced()) {
                level.setBlock(currentPos, ModBlocks.SMALL_SODIUM.get().defaultBlockState(), Block.UPDATE_ALL);
                this.kill();
            }
            // 情况3：落地位置既不是小钠块也不可替换，则掉落为物品实体
            else {
                this.spawnAtLocation(ModBlocks.SMALL_SODIUM.get());
                this.kill();
            }
        }else{
            if (!this.isNoGravity()) {
                this.setDeltaMovement(this.getDeltaMovement().add((double)0.0F, -0.04, (double)0.0F));
            }
            this.move(MoverType.SELF, this.getDeltaMovement());
        }
    }
    public void setStartPos(BlockPos blockpos) {
        this.entityData.set(SodiumEntity.DATA_START_POS, blockpos);
    }

    public BlockPos getStartPos() {
        return this.entityData.get(SodiumEntity.DATA_START_POS);
    }

    @Override
    public @NotNull BlockState getBlockState() {
        return blockState;
    }
}